# -*- coding: UTF-8 -*-

import datetime

from callbacks import Callback

class TodoList(Callback):
    def __init__(self):
        self.tasks = []

    def get_tasks(self):
        return self.tasks

    def add_task(self,task):
        self.tasks.append(task)
        _call_callbacks()

    def remove_task(self,task):
        self.tasks.remove(task)
        _call_callbacks()

class Todo(object):
    def __init__(self):
        self.context = u''
        self.completed = False

class Day(object):
    def __init__(self):
        # Events in the day
        self.events = []
        # Date
        self.day = datetime.date.today()

class Event(object):
    def __init__(self,time,type,name='',description=''):
        self.name = name
        self.type = type
        # datetime
        self.time = time
        self.has_alarm = False
        # Time and date the alarm should go off
        self.alarm_date_time = None
        self.description = description

    def set_alarm(self,time):
        self.has_alarm = True
        self.alarm_date_time = time
    
    def cancel_alarm(self):
        self.has_alarm = False
        self.alarm_date_time = None

class Calendar(Callback):
    def __init__(self):
        self.days = {}
        Callback.__init__(self)

    def add_event(self,event,date=datetime.datetime.today()):
        try:
            self.days[get_date(date)].events.append(event)
        except KeyError:
            day = Day()
            day.day = date
            day.events.append(event)
            self.days[get_date(date)] = day
        self.call_callbacks()
    
    def remove_event(self,event,date=datetime.datetime.today()):
        self.days[get_date(date)].events.remove(event)
        self.call_callbacks()

    def get_day_events(self,date):
        try:
            return self.days[get_date(date)].events
        except KeyError:
            return None

    def get_today_events(self):
        return self.get_day_events(datetime.datetime.today())

def get_date(date):
    return str(date.date())
